package cn.com.hhrcw.conferenceRoom.service.impl;

import cn.com.hhrcw.conferenceRoom.entity.ConferenceRoom;
import cn.com.hhrcw.conferenceRoom.mapper.ConferenceRoomMapper;
import cn.com.hhrcw.conferenceRoom.service.IConferenceRoomService;
import cn.com.hhrcw.service.HuaWeiRoomService;
import cn.com.hhrcw.utils.huawei.CreateMeetingParam;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 会议室 服务实现类
 * </p>
 *
 * @author shengli
 * @since 2021-09-18
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ConferenceRoomServiceImpl extends ServiceImpl<ConferenceRoomMapper, ConferenceRoom> implements IConferenceRoomService {

    @Autowired
    private HuaWeiRoomService huaWeiRoomService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Boolean createMeeting(ConferenceRoom conferenceRoom) {
        CreateMeetingParam createMeetingParam = new CreateMeetingParam();
        createMeetingParam.setLength(1440L);
        createMeetingParam.setSubject(conferenceRoom.getRoomName());
        createMeetingParam.setMediaTypes("HDVideo");
        createMeetingParam.setVmrFlag(1);
        JSONArray jsonArray = huaWeiRoomService.meetingList2(conferenceRoom.getHuaweId());
        Assert.notEmpty(jsonArray, "未获取到华为云会议室，请查验会议ID！");
        Assert.isFalse(jsonArray.size() > 1, "请输入准确的华为会议室ID！");
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        String meetingId = jsonObject.getString("id");

        Assert.isFalse(jsonObject.getInteger("status") != 0, "云会议室未分配或已停用！");
        createMeetingParam.setVmrID(meetingId);
        JSONObject meeting = huaWeiRoomService.createMeeting(createMeetingParam);
        Assert.notEmpty(meeting, "创建会议室失败！");
        conferenceRoom.setHostUrl(meeting.getString("chairJoinUri"));
        conferenceRoom.setPlayerUrl(meeting.getString("guestJoinUri"));
        conferenceRoom.setMeetingId(meetingId);
        conferenceRoom.setConferenceId(meeting.getString("conferenceID"));

        return this.saveOrUpdate(conferenceRoom);
    }

    @Override
    public Boolean cancelMeeting(ConferenceRoom conferenceRoom) {
        ConferenceRoom oldRoom = this.getById(conferenceRoom.getId());
        if (StrUtil.isNotBlank(oldRoom.getConferenceId())) {
            huaWeiRoomService.cancelMeeting(oldRoom.getConferenceId());
        }
        return this.lambdaUpdate().eq(ConferenceRoom::getId, conferenceRoom.getId())
            .set(ConferenceRoom::getHostUrl, null)
            .set(ConferenceRoom::getPlayerUrl, null)
            .set(ConferenceRoom::getMeetingId, null)
            .set(ConferenceRoom::getConferenceId, null)
            .set(ConferenceRoom::getStatus, 0)
            .set(ConferenceRoom::getIsPlatform, conferenceRoom.getIsPlatform())
            .set(ConferenceRoom::getCompanyId, conferenceRoom.getCompanyId())
            .set(ConferenceRoom::getEffectiveBeginTime, conferenceRoom.getEffectiveBeginTime())
            .set(ConferenceRoom::getEffectiveEndTime, conferenceRoom.getEffectiveEndTime())
            .set(ConferenceRoom::getUpdateBy, conferenceRoom.getUpdateBy())
            .set(ConferenceRoom::getUpdateTime, conferenceRoom.getUpdateTime()).update();
    }
}
